﻿namespace MetaProperties.Core
{
    using System;

    using MetaProperties.Framework;

#if ANCESTRY_SUPPORTED
    using MetaProperties.Framework.Ancestry;
#endif

    /// <summary>
    /// A read-only observable value.
    /// </summary>
    /// <typeparam name="T">The type of the contained value.</typeparam>
    public class ReadOnlyObservable<T> : TypedObservable<T>, MetaProperties.Framework.IObservable<T>
    {
        /// <summary>
        /// The contained read-only value.
        /// </summary>
        private readonly T value;

        /// <summary>
        /// Initializes a new instance of the <see cref="ReadOnlyObservable&lt;T&gt;"/> class.
        /// </summary>
        public ReadOnlyObservable()
            : this(Activator.CreateInstance<T>())
        { 
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ReadOnlyObservable&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        public ReadOnlyObservable(T value)
        {
            this.value = value;
#if ANCESTRY_SUPPORTED
            this.UpdateValueParent(value, UpdateParentOperation.Add);
#endif
        }

        /// <summary>
        /// Gets the value of the observable property.
        /// </summary>
        /// <value>The value of the observable property.</value>
        public T Value
        {
            get
            {
                return this.value;
            }
        }

        /// <summary>
        /// Gets the value of the observable property.
        /// </summary>
        /// <returns>The value of the observable property.</returns>
        protected override object GetValue()
        {
            return this.Value;
        }
    }
}
